﻿Imports KOR.Helpers
Imports KOR.Support
Imports Telerik.Web.UI

Public Class Products
    Inherits Telerik.Web.UI.RadAjaxPage

    Public Property ProductID As Integer
        Get
            Return CInt(hfProductID.Value)
        End Get
        Set(value As Integer)
            hfProductID.Value = value
        End Set
    End Property

    Public Property FileIds As String
        Get
            Return CStr(hfFileIds.Value)
        End Get
        Set(value As String)
            hfFileIds.Value = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ProductID = CInt(ValueOrDefault(Request.QueryString("pid"), 0))
            LoadProductData()
        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Try
            Dim dict As New Dictionary(Of String, Object)
            dict.Add("ProductName", txtProductName.Text)
            dict.Add("BrandID", ddlBrands.SelectedValue)
            If ddlCategory.SelectedIndex > 0 Then dict.Add("CategoryID", ddlCategory.SelectedValue) Else dict.Add("CategoryID", 0)
            If txtPrice.Text.ToString.Length > 0 Then dict.Add("Price", txtPrice.Text) Else dict.Add("Price", 0)
            If ddlPrice.SelectedIndex > 0 Then dict.Add("PriceRangeID", ddlPrice.SelectedValue) Else dict.Add("PriceRangeID", 0)
            If ddlLabelSize.SelectedIndex > 0 Then dict.Add("LabelSizeID", ddlLabelSize.SelectedValue) Else dict.Add("LabelSizeID", 0)
            If ddlCoreDiameter.SelectedIndex > 0 Then dict.Add("CoreDiameter", ddlCoreDiameter.SelectedValue) Else dict.Add("CoreDiameter", 0)
            dict.Add("PlasticHolderInclude", chkPlasticHolder.Checked)
            If ddlColor.SelectedIndex > 0 Then dict.Add("ColorID", ddlColor.SelectedValue) Else dict.Add("COlorID", 0)
            'dict.Add("TextColorID", ddlColorTape.SelectedValue)
            If ddlTapeLength.SelectedIndex > 0 Then dict.Add("TapeLenghtID", ddlTapeLength.SelectedValue) Else dict.Add("TapeLenghtID", 0)
            If ddlTextWidth.SelectedIndex > 0 Then dict.Add("TapeWidthID", ddlTextWidth.SelectedValue) Else dict.Add("TapeWidthID", 0)
            If ddlColorTape.SelectedIndex > 0 Then dict.Add("ColorTapeID", ddlColorTape.SelectedValue) Else dict.Add("ColorTapeID", 0)
            dict.Add("Description", reDesc.Content)
            If ddlTapeLength.SelectedIndex > 0 Then dict.Add("Length", ddlTapeLength.SelectedValue) Else dict.Add("Length", 0)
            If ddlTextWidth.SelectedIndex > 0 Then dict.Add("Width", ddlTextWidth.SelectedValue) Else dict.Add("Width", 0)
            dict.Add("PrinterSupport", radPrinterSupport.Content)
            dict.Add("NonCompetiblePrinter", reNonCompetible.Content)
            dict.Add("SKU", txtSKU.Text)
            dict.Add("NoOfLabelsPerRoll", txtNoOfLabels.Text)
            dict.Add("ReOrderLevel", txtReOrderLevel.Text)

            Dim FileID As Integer = 0
            If radUpload.UploadedFiles.Count > 0 Then
                Dim FileIds As String = ""

                For Each fl As UploadedFile In radUpload.UploadedFiles
                    FileID = BL.Files.SaveFile(fl, "Products/Pictures")
                    FileIds = FileIds.ConcatCSV(FileID, ",")
                Next

                If ProductID > 0 Then
                    FileIds = FileIds.ConcatCSV(BL.Products.GetFileIdsByProductID(ProductID), ",")
                End If

                dict.Add("FileIDs", FileIds)
            End If



            If ProductID > 0 Then
                dict.Add("LastModifiedDate", DateTime.Now)
                'dict.Add("LastModifiedByID", UserID)
                BL.Products.UpdateProduct(dict, ProductID)
            Else
                dict.Add("CreatedDate", DateTime.Now)
                'dict.Add("CreatedByID", UserID)
                BL.Products.InsertProduct(dict)
            End If

            rgProducts.DataBind()
            ClearControls()

        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
    End Sub

    Sub ClearControls()
        txtProductName.Text = ""
        ddlBrands.SelectedIndex = 0
        ddlCategory.SelectedIndex = 0
        ddlPrice.SelectedIndex = 0
        ddlPrice.SelectedIndex = 0
        ddlLabelSize.SelectedIndex = 0
        ddlCoreDiameter.SelectedIndex = 0
        chkPlasticHolder.Checked = False
        ddlColor.SelectedIndex = 0
        ddlColor.SelectedIndex = 0
        ddlTapeLength.SelectedIndex = 0
        ddlColorTape.SelectedIndex = 0
        reDesc.Content = ""
        ddlTapeLength.SelectedIndex = 0
        ddlTextWidth.SelectedIndex = 0
        radPrinterSupport.Content = ""
        LoadPictures("0")
    End Sub



    Private Sub rgProducts_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles rgProducts.ItemCommand
        Try
            If e.CommandName = "ShowDetails" Then
                Response.Redirect("~/admin/Products.aspx?pid=" & e.CommandArgument)
            End If
        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
    End Sub

    Sub LoadProductData()
        Try
            Using reader As IDataReader = BL.Products.GetProductByProductID(ProductID)
                If reader.Read Then
                    txtProductName.Text = ValueOrDefault(reader("ProductName"), "").ToString
                    txtSKU.Text = ValueOrDefault(reader("SKU"), "").ToString
                    ddlBrands.SelectedIndex = ValueOrDefault(ddlBrands.Items.IndexOf(ddlBrands.Items.FindByValue(reader("BrandID"))), "0")
                    ddlCategory.SelectedIndex = ValueOrDefault(ddlCategory.Items.IndexOf(ddlCategory.Items.FindByValue(reader("CategoryID"))), "0")
                    ddlPrice.SelectedIndex = ValueOrDefault(ddlPrice.Items.IndexOf(ddlPrice.Items.FindByValue(reader("PriceRangeID"))), "0")
                    txtPrice.Text = ValueOrDefault(reader("Price"), 0).ToString

                    ddlLabelSize.SelectedIndex = ValueOrDefault(ddlLabelSize.Items.IndexOf(ddlLabelSize.Items.FindByValue(reader("LabelSizeID"))), "0")
                    ddlCoreDiameter.SelectedIndex = ValueOrDefault(ddlCoreDiameter.Items.IndexOf(ddlCoreDiameter.Items.FindByValue(reader("CoreDiameter"))), "0")
                    chkPlasticHolder.Checked = CBool(ValueOrDefault(reader("PlasticHolderInclude"), False))
                    ddlColor.SelectedIndex = ValueOrDefault(ddlColor.Items.IndexOf(ddlColor.Items.FindByValue(reader("ColorID"))), "0")
                    ddlColorTape.SelectedIndex = ValueOrDefault(ddlColorTape.Items.IndexOf(ddlColorTape.Items.FindByValue(reader("ColorTapeID"))), "0")
                    ddlTapeLength.SelectedIndex = ValueOrDefault(ddlTapeLength.Items.IndexOf(ddlTapeLength.Items.FindByValue(reader("TapeLenghtID"))), "0")
                    ddlTextWidth.SelectedIndex = ValueOrDefault(ddlTextWidth.Items.IndexOf(ddlTextWidth.Items.FindByValue(reader("TapeWidthID"))), "0")

                    

                    txtReOrderLevel.Text = ValueOrDefault(reader("ReOrderLevel"), 0).ToString
                    txtSKU.Text = ValueOrDefault(reader("SKU"), "").ToString
                    txtNoOfLabels.Text = ValueOrDefault(reader("NoOfLabelsPerRoll"), 0).ToString

                    reDesc.Content = ValueOrDefault(reader("Description"), "").ToString
                    radPrinterSupport.Content = ValueOrDefault(reader("PrinterSupport"), "").ToString
                    reNonCompetible.Content = ValueOrDefault(reader("NonCompetiblePrinter"), "").ToString

                    If ValueOrDefault(reader("FileIds"), "").ToString() <> "" Then

                        FileIds = ValueOrDefault(reader("FileIds"), "").ToString()
                        LoadPictures(ValueOrDefault(reader("FileIds"), "").ToString())
                    End If

                End If
                reader.Close()
            End Using

        Catch ex As Exception

        End Try
    End Sub

    Sub LoadPictures(Ids As String)
        dlstPictures.DataSource = BL.Files.GetFiles(Ids)
        dlstPictures.DataBind()
    End Sub

    Private Sub dlstPictures_ItemCommand(source As Object, e As DataListCommandEventArgs) Handles dlstPictures.ItemCommand
        Try
            If e.CommandName = "Delete" Then
                If BL.Files.DeleteFile(CInt(e.CommandArgument)) > 0 Then
                    FileIds = FileIds.Replace(e.CommandArgument, 0)
                    LoadPictures(FileIds)
                End If
            End If
        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
    End Sub

    Private Sub dlstPictures_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles dlstPictures.ItemDataBound
        Try
            Dim rbi As RadBinaryImage = CType(e.Item.FindControl("rbiImage"), RadBinaryImage)
            rbi.DataValue = ValueOrDefault(DataBinder.Eval(e.Item.DataItem, "FileData"), Nothing)
        Catch ex As Exception
            WebMsgBox.ShowError(ex.Message)
        End Try
    End Sub
End Class